package com.jun.admin.core.listener;


import com.jun.admin.core.model.constant.RedisKeyConstant;
import com.jun.common.redis.pubsub.RedisConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author yuejun.li
 * @date 2024/8/13 16:25:57
 * redis的消息发布
 */
@Component
@Slf4j
public class RedisMessageListener extends RedisConsumer {

    /**
     * 订阅频道
     * @param container
     */
    public RedisMessageListener(RedisMessageListenerContainer container){
        super(container);
        subscribe(RedisKeyConstant.REDIS_JUN_CHANNLE);
    }

    @Override
    protected void onReceiveMessage(Message message, byte[] pattern) {
        //消息通道
        String channel = new String(message.getChannel());
        //消息内容
        String messageBody = new String(message.getBody(), StandardCharsets.UTF_8);
        //消息订阅的匹配规则，如 new PatternTopic("test-*") 中的 test-*
        String msgPattern = new String(pattern);
        log.info("接收消息: channel={} body={} pattern={} ", channel, messageBody, msgPattern);
        // 这里处理接收的消息
    }
}
